<?php

/**f
 * @author christian
 * @copyright 2011
 *  
 */
 

 //establecer una variable conratafisica $_LOCALHOST
$ruta=$_SERVER['DOCUMENT_ROOT']."/viventura"."/MDTour";
include_once("$ruta/star.php");
include_once("$ruta/MDTourGeneral/CDrawPageReg.php");
//include_once("$rut/conf/CConfig.php");


/** Datos del paquete-tour  */
 
if (!isset($_POST['multi'])) {$_POST['multi'] = "NULL"; }
if (!isset($_GET['default'])) {$_GET['default'] = "NULL"; }
if (!isset($_GET['basic'])) {$_GET['basic'] = "NULL"; }
if (!isset($_POST['idPack'])) {$_POST['idPack'] = "NULL"; }
if (!isset($_POST['idTour'])) {$_POST['idTour'] = "NULL"; }
if (!isset($_POST['idPackTour'])) {$_POST['idPackTour'] = "NULL"; }
if (!isset($_POST['idOpe'])) {$_POST['idOpe'] = "NULL"; }



$multiple = $_POST['multi'];
$default=$_GET['default'];
$basic=$_GET['basic'];
$idPack=$_POST['idPack'];
$idTour=$_POST['idTour'];
$idPackTour=$_POST['idPackTour'];

$idOpe=$_POST['idOpe'];


/** Datos del paquete  */

if (!isset($_POST['NomPack'])) {$_POST['NomPack'] = "NULL"; }
if (!isset($_POST['CostPerOne'])) {$_POST['CostPerOne'] = "NULL"; }
if (!isset($_POST['CostPerTwo'])) {$_POST['CostPerTwo'] = "NULL"; }
if (!isset($_POST['IntComPack'])) {$_POST['IntComPack'] = "NULL"; }
if (!isset($_POST['ExtComPack'])) {$_POST['ExtComPack'] = "NULL"; }
if (!isset($_POST['NotProcPax'])) {$_POST['NotProcPax'] = "NULL"; }
if (!isset($_POST['OFICINAS_idOfi'])) {$_POST['OFICINAS_idOfi'] = "NULL"; }
if (!isset($_POST['AvaPack'])) {$_POST['AvaPack'] = "NULL"; }

$NomPack  = $_POST['NomPack'];
$CostPerOne  = $_POST['CostPerOne'];
$CostPerTwo  = $_POST['CostPerTwo'];
$IntComPack  = $_POST['IntComPack'];
$ExtComPack  = $_POST['ExtComPack'];
$NotProcPax  = $_POST['NotProcPax'];
$OFICINAS_idOfi  = $_POST['OFICINAS_idOfi'];
$AvaPack  = $_POST['AvaPack'];





/** Datos del tc */

if (!isset($_POST['idOfi'])) {$_POST['idOfi'] = "NULL"; }
if (!isset($_POST['idGuide1'])) {$_POST['idGuide1'] = "NULL"; }
if (!isset($_POST['idGuide1'])) {$_POST['idGuide2'] = "NULL"; }

$idOfi = $_POST['idOfi'];
$idGuide1 = $_POST['idGuide1'];
//$idGuide2 = $_POST['idGuide2'];


/** Oficina y operador */

$query = "select OPERADOR_idOpe from oficinas where idOfi=$idOfi";
$result= CModelo::getInstance()->getData($query);
foreach($result as $r=>$c){
    $result2 = $c;
    foreach($result2 as $r2=>$c2){
        $idOpe=$c2;
    }   
}
/** Datos del tour */


    
if (!isset($_POST['from'])) {$_POST['from'] = "NULL"; }
if (!isset($_POST['to'])) {$_POST['to'] = "NULL"; }
if (!isset($_POST['PriTour'])) {$_POST['PriTour'] = "NULL"; }
if (!isset($_POST['MinPax'])) {$_POST['MinPax'] = "NULL"; }
if (!isset($_POST['MaxPax'])) {$_POST['MaxPax'] = "NULL"; }
if (!isset($_POST['OriTour'])) {$_POST['OriTour'] = "NULL"; }
if (!isset($_POST['DestTour'])) {$_POST['DestTour'] = "NULL"; }

$DateBegTour = $_POST['from'];
$uno = CDrawPageReg::getInstance()->amysql($DateBegTour);


$DateEndTour = $_POST['to'];
$dos = CDrawPageReg::getInstance()->amysql($DateEndTour);


$PriTour =$_POST['PriTour'];
$MinPax =$_POST['MinPax'];
$MaxPax=$_POST['MaxPax'];
$OriTour =$_POST['OriTour'];
$DestTour =$_POST['DestTour'];



if($default==1){
    
    
        if($basic==1){
               $querybasic = "insert into pack (idPack,NomPack,CostPerOne,CostPerTwo,IntComPack,ExtComPack,NotProcPax,OFICINAS_idOfi,AvaPack) 
               values ('NULL', '$NomPack','$CostPerOne','$CostPerTwo','$IntComPack','$ExtComPack','$NotProcPax','$idOfi','1')";
                
               $idPack = CModelo::getInstance()->insertData($querybasic);
                //echo $idPack."/";
        }
    
        $query = "insert into tour (idTour, OriTour, DestTour, PriTour, MinPaxTour, MaxPaxTour) values ('NULL', '$OriTour', '$DestTour', '$PriTour', '$MinPax', '$MaxPax')";
        $idTourCopy = CModelo::getInstance()->insertData($query);
        //echo $query;
        //echo $idTourCopy."/";
            
        $query = "insert into pack_has_tour (idPackTour,PACK_idPack,TOUR_idTour,DateBegTour,DateEndTour,DefaultTourPack) values ('NULL', '$idPack','$idTourCopy','$uno','$dos',1)";

        $idPackTour = CModelo::getInstance()->insertData($query);
    //    echo $idPackTour."/";
    
        $query = "insert into pack_has_tour_has_guidetc (idPackTourGui,guidetc_idGuide,pack_has_tour_idPackTour) values ('NULL','$idGuide1','$idPackTour'),('NULL','$idGuide2','$idPackTour')";
        $idPackTourGui = CModelo::getInstance()->insertData($query);
        //echo $idPackTourGui."/";
               
        //registrando extensiones
        if($multiple!="NULL"){
        foreach($multiple as $r=>$c){ 
            $query = "insert into pack_has_tour_has_extensiones (idExtTour,pack_has_tour_idPackTour,extensiones_idExt,operador_idOpe) values ('NULL','$idPackTour','$c','$idOpe')";
        //    echo $query;
            $extension = CModelo::getInstance()->insertData($query);
        }}
                
        echo "<script> parent.location.href = '/viventura/MDTour/index.php?updated=$idPack' </script>"; 
            }

else{
    //////// Personalizar el nombre del tour, se pasa del NamTypTour (default) a campo personalizado en pack_has_tour//////////
      if (!isset($_POST['NomPack'])) {$_POST['NomPack'] = "NULL"; }
      $NamTour  = $_POST['NomPack'];
      
      
     //echo $idPackTour;  
     $query = "insert into pack_has_tour (idPackTour,NamTour,PACK_idPack,TOUR_idTour,DateBegTour,DateEndTour,DefaultTourPack) values ('NULL','$NamTour', '$idPack','$idTour','$uno','$dos',0)";
     $idPackTourNew = CModelo::getInstance()->insertData($query);
     

     //echo $idPackTourNew;
     copying_tracks($idPackTour,$idPackTourNew);     
    //echo $query;
    

     if($multiple=="NULL"){
        
        $query = "delete from pack_has_tour_has_extensiones where pack_has_tour_idPackTour=$idPackTourNew";
        CModelo::getInstance()->insertData($query);
 
        }
     else{
        $query = "delete from pack_has_tour_has_extensiones where pack_has_tour_idPackTour=$idPackTourNew";
        CModelo::getInstance()->insertData($query);
        //echo $query;
           

              
        foreach($multiple as $r=>$c){ 
        
        $query = "insert into pack_has_tour_has_extensiones (idExtTour,pack_has_tour_idPackTour,extensiones_idExt,operador_idOpe) values ('NULL','$idPackTour','$c','$idOpe')";
        //echo $query;
        $extension = CModelo::getInstance()->insertData($query);
        }
        

       
        
        //echo "<script> parent.location.href = '/viventura/MDTour/index.php?updated=$idPack' </script>";
       
    }
    
      
        echo "<script> parent.location.href = 'MDPasajeros/MDPasajeros.php?codigo=$idPackTourNew' </script>";
 
  
}
        
 


function copying_tracks($oldTourPack,$newTourPack){

    $tracks = 0; //Inicialmente se supone que existen 0 tracks en el paquete base
    
    
    $query = "select OrdTrack from tour_has_track where TOUR_idTour='$oldTourPack' ORDER BY OrdTrack DESC LIMIT 0, 1";
    $duration = CModelo::getInstance()->getData($query);
    
    foreach($duration as $r => $c){
        $table1 = $c;
        foreach($table1 as $r1 => $c1){
                $tracks = $c1;    
        }            
    }
    //echo $tracks;
    $o=1;
    if($tracks>0){
        
        while($o<=$tracks){
        $query = "select idTourTrack,TOUR_idTour,TRACK_idTrack,RESTAURANT_idRest,OrdTrack,DateBegTrack,DateEndTrack 
                from tour_has_track where Tour_idTour = $oldTourPack and OrdTrack=$o";            
        //echo $query;
        $result = CModelo::getInstance()->getData($query);
    
           foreach($result as $r=>$c){
            $table1=$c;
                foreach($table1 as $r2=>$c2){
                    $table2[] = $c2;
                }
            }
            //echo $table2[6];
        
       // var_dump($table2);
        
        $insert = "insert into tour_has_track (idTourTrack,TOUR_idTour,TRACK_idTrack,RESTAURANT_idRest,OrdTrack,DateBegTrack,DateEndTrack) 
                   values (NULL,'$newTourPack','$table2[2]','$table2[3]','$table2[4]','$table2[5]','$table2[6]')";
        $copyTrack = CModelo::getInstance()->insertData($insert);
         //echo $insert;    
        $o++;
        $table2="";
        }
    }
    else{ //actualizacion del paquete
        
        
        
     }
     //var_dump($table1);
    
    
}





?>